package com.example.es.service;

import com.example.es.business.user.dto.SysUserDTO;
import org.elasticsearch.index.query.PrefixQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author WangKun
 * @create 2019-01-11
 * @desc
 **/
@Service
public class UserService {

    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;

    public List<SysUserDTO> list(Pageable pageable) {
        PrefixQueryBuilder queryBuilder = QueryBuilders.prefixQuery("loginName", "1003224");
//        TermQueryBuilder queryBuilder = QueryBuilders.termQuery("loginName", "10000001");

        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryBuilder)
                .withPageable(pageable).build();
        List<SysUserDTO> sysUserDTOS = elasticsearchTemplate.queryForList(searchQuery, SysUserDTO.class);
        return sysUserDTOS;
    }
}
